import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:math' as math;

const _kScrollHeadHeight = 100.0;

class LaunchCharityController extends GetxController {
  // 滚动相关状态
  final ScrollController scrollController = ScrollController();
  double scrollOpacity = 0.0;
  bool isDark = true;

  // 基本信息
  String charityName = '';
  DateTime? startTime;
  DateTime? endTime;
  String fundraisingGoal = '';
  String charityPurpose = '';

  // 项目信息
  String organizationName = '';
  String serviceDescription = '';
  String charityProcess = '';
  String needsAndUses = '';

  // 图片
  List<String> coverImages = [];
  List<String> relatedImages = [];

  @override
  void onInit() {
    super.onInit();
    scrollController.addListener(_onScroll);
  }

  @override
  void onClose() {
    scrollController.dispose();
    super.onClose();
  }

  void _onScroll() {
    final offset = scrollController.offset;
    final opacity = (math.min(1, math.max(0, offset / _kScrollHeadHeight))).toDouble();
    final isDark = offset < _kScrollHeadHeight;

    if (opacity != scrollOpacity || isDark != this.isDark) {
      scrollOpacity = opacity;
      this.isDark = isDark;
      update();
    }
  }

  // 时间选择器相关
  void handleDateTimeRangeSelected(DateTime? start, DateTime? end) {
    if (start != null && end != null) {
      if (start.isAfter(end)) {
        SmartDialog.showToast('起始时间不能晚于截止时间');
        startTime = null;
        endTime = null;
        return;
      }
      startTime = start;
      endTime = end;
      update();
    }
  }

  // 基本信息更新方法
  void updateCharityName(String value) {
    charityName = value;
    update();
  }

  void updateFundraisingGoal(String value) {
    fundraisingGoal = value;
    update();
  }

  void updateCharityPurpose(String value) {
    charityPurpose = value;
    update();
  }

  // 项目信息更新方法
  void updateOrganizationName(String value) {
    organizationName = value;
    update();
  }

  void updateServiceDescription(String value) {
    serviceDescription = value;
    update();
  }

  void updateCharityProcess(String value) {
    charityProcess = value;
    update();
  }

  void updateNeedsAndUses(String value) {
    needsAndUses = value;
    update();
  }

  // 图片处理相关
  void handleCoverImagePicked(ImageSource source) async {
    // TODO: 处理封面图片选择逻辑
    update();
  }

  void handleCoverImageDelete(int index) {
    coverImages.removeAt(index);
    update();
  }

  void handleRelatedImagePicked(ImageSource source) async {
    // TODO: 处理相关图片选择逻辑
    update();
  }

  void handleRelatedImageDelete(int index) {
    relatedImages.removeAt(index);
    update();
  }

  // 表单验证
  bool validateForm() {
    if (charityName.isEmpty) {
      SmartDialog.showToast('请输入益事名称');
      return false;
    }
    if (startTime == null || endTime == null) {
      SmartDialog.showToast('请选择益事时间');
      return false;
    }
    if (fundraisingGoal.isEmpty) {
      SmartDialog.showToast('请输入筹集目标');
      return false;
    }
    if (charityPurpose.isEmpty) {
      SmartDialog.showToast('请输入益事目的');
      return false;
    }
    if (organizationName.isEmpty) {
      SmartDialog.showToast('请输入发起机构');
      return false;
    }
    if (serviceDescription.isEmpty) {
      SmartDialog.showToast('请输入服务对象描述');
      return false;
    }
    if (charityProcess.isEmpty) {
      SmartDialog.showToast('请输入益事流程');
      return false;
    }
    if (needsAndUses.isEmpty) {
      SmartDialog.showToast('请输入需求及用途');
      return false;
    }
    if (coverImages.isEmpty) {
      SmartDialog.showToast('请上传封面背景图');
      return false;
    }
    return true;
  }

  // 提交表单
  void submitForm() {
    if (!validateForm()) return;
    
    // TODO: 处理表单提交逻辑
    SmartDialog.showToast('提交成功');
  }
} 